﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class News : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private Int32 DanhMucID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 30;
        private Int32 totalRecord;
        private String Search = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    //lấy tất cả danh sách chuyên mục tin tức
                    dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0];

                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }

                    try
                    {
                        DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());
                    }
                    catch
                    {
                        DanhMucID = -1;
                    }

                    try
                    {
                        Search = Request.QueryString["Search"].ToString();
                    }
                    catch
                    {
                        Search = "";
                    }

                    try
                    {
                        pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
                    }
                    catch
                    {
                        pageIndex = 1;
                    }

                    try
                    {
                        pageSize = Int32.Parse(Request.QueryString["PageSize"].ToString());
                        Session["PageSize"] = pageSize.ToString();
                    }
                    catch
                    {
                        pageSize = 30;
                    }

                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    LoadLanguage(Thread.CurrentThread.CurrentCulture);
                    LoadTinTuc(DanhMucID);                   

                    CreateDDLCategory(0, 0);
                    if (IsVN == true)
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                    else
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                    ddlDanhmucSanPham.SelectedValue = DanhMucID.ToString();
                }
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {            

        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if (IsVN == true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhmucSanPham.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        /// <summary>
        /// Hàm lấy tất cả các tin
        /// </summary>
        /// <returns></returns>
        public TinTuc GetTinTuc()
        {
            TinTuc theTinTuc = new TinTuc();
            theTinTuc.DanhMucTinTucID = DanhMucID;
            //lấy theo ngôn ngữ mặc định
            if (IsVN == true)
            {
                theTinTuc.TieuDeVN = Search;
                theTinTuc.TieuDeOther = "";
            }
            //lấy theo ngôn ngữ khác
            else
            {
                theTinTuc.TieuDeOther = Search;
                theTinTuc.TieuDeVN = "";
            }

            return theTinTuc;
        }
        /// <summary>
        /// Hàm lấy danh mục tin theo ID
        /// </summary>
        /// <param name="DanhMucID"></param>
        public void LoadTinTuc(Int32 DanhMucID)
        {
            //lấy theo ngôn ngữ mặc định
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                rpNews.DataSource = TinTucProvider.GetTinTucsByFillter(GetTinTuc(), pageIndex, pageSize, out totalRecord);
                rpNews.DataBind();
                pnTinTucOther.Visible = false;
            }
            //lấy theo ngôn ngữ khác
            else
            {
                rpNewsOther.DataSource = TinTucProvider.GetTinTucsByFillter(GetTinTuc(), pageIndex, pageSize, out totalRecord);
                rpNewsOther.DataBind();
                pnTinTuc.Visible = false;
            }
            //phân trang 
            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "News.aspx?DanhMucID=" + DanhMucID.ToString();
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("News.aspx?DanhMucID=" + ddlDanhmucSanPham.SelectedValue);
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("News.aspx?DanhMucID=" + DanhMucID + "&&Search=" + txtsearch.Text);
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (languageCode == "vi-VN")
            {
                foreach (RepeaterItem rp in rpNews.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpNewsOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            if (arr.Count > 1 || arr.Count == 0)
            {
                if (IsVN == true)
                    ltrErorr.Text = HangSo.Eorr_Edit;
                else
                    ltrErorr.Text = HangSo.Eorr_EditOther;
            }
            else
            {
                Session["value"] = Int32.Parse(arr[0].ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "GetRadWindow().close()", true);
            }
        }
    }
}